<template>
	<view class="content">
		<nav-bars backState="3000" :bgColor="navBackground" fontColor="#fff" title="兑换码" @tap="backClick()" ></nav-bars>
		<view class="wrap">
			<view class="main-box" :style="{background: 'url('+bg+')',backgroundSize:'cover'}">
                 <view class="name">
					 {{prize_name}}
				 </view>
				 <view class="time">
					 有效期至：{{formatDate(end_time)}}
				 </view>
				 <view class="qrcode">
					<image :src="code"></image>
					 <text>序列号：{{prize_code}}</text>
				 </view>
			</view>
		</view>
	</view>
</template>

<script>
	var t;
	import activity from '@/service/activity.js'
	export default {
		data() {
			return {
				navBackground: '',
				imageSrc: this.$static,
				bg: this.$static + '/static/activity/qrcode_bg.png',
				prize_id:'',
				prize_code:'',
				end_time:'',
				prize_name:'',
				code:'',
			}
		},
		onLoad(option) {
           this.$utils.getUrl();
		   this.prize_id=option.id;
		   this.prize_code=option.code;
		   this.end_time=option.end_time;
		   this.prize_name=option.title;
		},
		onShow() {
			this.getprizeWriteOff();
		},
		methods: {
			
			backClick(){
				clearTimeout(t)
				uni.navigateBack();
			},
			goSkip(url) {
				this.$common.navigateTo(url);
			},
			//奖品核销
			getprizeWriteOff() {
				uni.showLoading()
				activity.prizeWriteOff({
						prize_id:this.prize_id,
						prize_code:this.prize_code,
						user_id: uni.getStorageSync('userId'),
					})
					.then(res => {
						uni.hideLoading()
						let _this = this;
						if (res.code == 1 || res.code == 0) {
							_this.code = res.msg;
							t = setTimeout(function() {
								_this.writeOffStatus(_this.prize_code);
							}, 3000);
			
						} else {
							_this.$common.errorToShow(res.msg)
						}
					})
					.catch(err => {
						uni.hideLoading()
					})
			},
			formatDate(value) {
				let date = new Date(parseInt(value) * 1000);
				let y = date.getFullYear();
				let m = date.getMonth() + 1;
				m = m < 10 ? "0" + m : m;
				let d = date.getDate();
				d = d < 10 ? "0" + d : d;
				let h = date.getHours();
				h = h < 10 ? "0" + h : h;
				let minute = date.getMinutes();
				let second = date.getSeconds();
				minute = minute < 10 ? "0" + minute : minute;
				second = second < 10 ? "0" + second : second;
				return y + "-" + m + "-" + d;
			},
			//核销验证
			writeOffStatus(code) {
				activity.prizeValid({
						prize_code: code,
						//user_id: uni.getStorageSync('userId'),
					})
					.then(res => {
						let _this = this;
						if (res.code == 1) {
							_this.$common.errorToShow('核销成功');
							clearTimeout(t)
							setTimeout(function() {
								uni.reLaunch({
								    url: '/pages_activity/pages/lotteryDraw/writeOff/result?status=1'
								});
							}, 2000);
						} else {
							t = setTimeout(function() {
								_this.writeOffStatus(code);
							}, 3000);
							//_this.$common.errorToShow(res.msg)
						}
					})
					.catch(err => {
			
					})
			},
			goSkip(url){
				 this.$common.navigateTo(url);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100vh;
		background-color: #E3672C;

		.wrap {
			.main-box {
				padding: 39rpx;
				width: 92.67%;
				height: 883rpx;
				margin:64rpx auto;
				.name{
					font-size: 38rpx;
					padding-top: 40rpx;
					font-weight: bold;
					color: #201B18;
					margin-bottom: 10rpx;
				}
				.time{
					width: 363rpx;
					height: 25rpx;
					font-size: 26rpx;
					font-weight: 500;
					color: #8A8583;
				}
				.qrcode{
					margin-top: 140rpx;
					text-align: center;
					image{
						width: 375rpx;
						height: 374rpx;
						margin: auto;
					}
					text{
						color: #201B18;
						line-height:90rpx;
					}
				}
			}
		}
	}
</style>
